/*
 * 
 */
package es.drOsu.game;

import java.util.ArrayList;
import java.util.List;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.openfeint.api.OpenFeint;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import es.drOsu.common.Constants;
import es.drOsu.common.SongsSQLiteHelper;
import es.drOsu.common.Tools;

// TODO: Auto-generated Javadoc
/**
 * The Class Game donde se visualizaran todas las canciones ya jugadas por el jugador y donde eligira para jugarlas de nuevo.
 */
public class Game extends ListActivity {


	/** The Constant TAG para debug en el LogCat. */
	private static final String TAG = "Game";
	 
	/** The path files listado con todas las canciones(artista y titulo). */
	List<String> pathFiles = new ArrayList<String>();
	
	/** The game mode. */
	private int gameMode = -1;
	
	/** The game difficulty. */
	private int gameDifficulty = -1;
	
	/** The user. */
	private String user = null;
	
	/** The paths listado con todas las rutas de las canciones. */
	private String [] paths = null;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.game);
		SharedPreferences prefs = getSharedPreferences(OpenFeint.getCurrentUser().userID(),Context.MODE_PRIVATE);
		if(prefs.getString(Constants.sharedPreferencesSoundsEffects, Constants.sharedPreferencesSoundEffectsUncheck).contains(Constants.sharedPreferencesSoundEffectsUncheck)){
			Log.d(TAG, "sound uncheck");
		}else Log.d(TAG, "sound check");
		Bundle bundle = this.getIntent().getExtras();
		gameMode = bundle.getInt(Constants.keyGameMode);
		user = bundle.getString(Constants.keyUser);
		gameDifficulty = bundle.getInt(Constants.keyGameDifficulty);
		paths = SongsSQLiteHelper.getPathSongs(this, user);
		for(int i = 0;i<paths.length;i++){
			es.drOsu.common.Song song=new es.drOsu.common.Song(paths[i]);
            //Log.d(TAG, "artista: "+song.artist+" title: "+song.title + " path: "+file.getPath());
           pathFiles.add(song.title+" - "+song.artist);
		}
		ArrayAdapter<String> songList= new ArrayAdapter<String>(this, R.layout.row, pathFiles);
		setListAdapter(songList);
		
		AdView adView = (AdView)this.findViewById(R.id.AdMob);
        AdRequest request = new AdRequest();
        //request.addTestDevice(AdRequest.TEST_EMULATOR);
        //request.addTestDevice("527E3725F773AF4EB8D7BE441914A139");
        request.setTesting(true);
       
        adView.loadAd(request);
	}
	
	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.d(TAG, "path: " + pathFiles.get(position));
		if(Tools.checkConnectivity(this)){
			try {
				Intent intent = new Intent(this, GameNewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString(Constants.keyPath, paths[position]);
				bundle.putString(Constants.keyUser, user);
				bundle.putInt(Constants.keyGameMode, gameMode);
				bundle.putInt(Constants.keyGameDifficulty, gameDifficulty);
				intent.putExtras(bundle);
				startActivity(intent);
				
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}else{
			Intent mainIntent = new Intent(this, DrosuMain.class);
			startActivity(mainIntent);
		}
			
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause(){
		super.onPause();
		Log.d(TAG, "onPause()");
		finish();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onRestart()
	 */
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "onRestart");
		finish();
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;

	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.add_new_song:
				Intent intent = new Intent(this, Songs.class);
				Bundle bundle = new Bundle();
				bundle.putString(Constants.keyUser, user);
				bundle.putInt(Constants.keyGameMode, gameMode);
				bundle.putInt(Constants.keyGameDifficulty, gameDifficulty);
				intent.putExtras(bundle);
				startActivity(intent);
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			Intent intentMain = new Intent(this,DrosuMain.class);			 
			 this.startActivity(intentMain);
			return true;
		}else
			return super.onKeyDown(keyCode, event);
	}
}
